package com.chendx.store.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.order.pojo.Order;
import com.chendx.model.user.UserInfoVo;
import com.chendx.model.user.dto.UserDto;
import com.chendx.model.user.pojo.User;
import com.chendx.store.user.feign.BehaviorFeign;
import com.chendx.store.user.feign.OrderFeign;
import com.chendx.store.user.mapper.UserMapper;
import com.chendx.store.user.service.UserService;
import com.chendx.store.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by chendx on 2022/4/19.
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private BehaviorFeign behaviorFeign;
    @Autowired
    private OrderFeign orderFeign;
    @Value("${fdfs.url}")
    private String fdfsUrl;
    @Value("${socket.host}")
    private String socketHost;

    @Override
    public ResponseResult loadUserInfo() {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        Integer focusCount = 0;
        Integer collectCount = 0;
        Integer noPayNum = 0;
        Integer noConNum = 0;
        Integer noGoodsNum = 0;
        Integer noPjNum = 0;
        UserInfoVo info = new UserInfoVo();
        try {
            focusCount = Integer.valueOf(behaviorFeign.focusCount().getData().toString());
            collectCount = Integer.valueOf(behaviorFeign.collectCount().getData().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            focusCount = 0;
            collectCount = 0;
        }
        ResponseResult responseResult = null;
        try {
            responseResult = orderFeign.loadUserAllOrder();
        } catch (Exception e) {
            e.printStackTrace();
            noPayNum = 0;
            noConNum = 0;
            noGoodsNum = 0;
            noPjNum = 0;
        }
        if (responseResult != null && responseResult.getData() != null){
            List<Order> orderList = JSON.parseArray(JSON.toJSONString(responseResult.getData()),Order.class);
            noPayNum = Math.toIntExact(orderList.stream().filter(order -> order.getPayStatus().equals("0")).count());
            noConNum = Math.toIntExact(orderList.stream().filter(order -> order.getPayStatus().equals("1") && order.getConsignStatus().equals("0")).count());
            noGoodsNum = Math.toIntExact(orderList.stream().filter(order -> order.getPayStatus().equals("1")
                    && order.getConsignStatus().equals("1")).count());
            noPjNum = Math.toIntExact(orderList.stream().filter(order -> order.getPayStatus().equals("1")
                    && order.getConsignStatus().equals("2") && order.getOrderStatus().equals("1")).count());
        }
        info.setCollectCount(collectCount);
        info.setFocusCount(focusCount);
        info.setNoConNum(noConNum);
        info.setNoGoodsNum(noGoodsNum);
        info.setNoPayNum(noPayNum);
        info.setNoPjNum(noPjNum);
        info.setUsername(username);
        return ResponseResult.okResult(info);
    }

    @Override
    public ResponseResult<UserInfoVo> loadBaseInfo() {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUsername(username);
        userInfoVo.setHeadImg(user.getHeadPic());
        ResponseResult responseResult = ResponseResult.okResult(userInfoVo);
        responseResult.setHost(fdfsUrl);
        responseResult.setSocket(socketHost);
        return responseResult;
    }

    @Override
    public UserInfoVo loadUser(String userId) {
        if (userId == null) {
            return null;
        }
        User u = getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setHeadImg(fdfsUrl + u.getHeadPic());
        userInfoVo.setUsername(u.getUsername());
        return userInfoVo;
    }

    @Override
    public ResponseResult updateInfo(UserDto dto) {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (!dto.getUserId().equals(user.getId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (!StringUtils.isEmpty(dto.getHeadImg())){
            user.setHeadPic(dto.getHeadImg());
        }
        if (!StringUtils.isEmpty(dto.getName())){
            user.setName(dto.getName());
        }
        if (!StringUtils.isEmpty(dto.getNickname())){
            user.setNickName(dto.getNickname());
        }
        if (!StringUtils.isEmpty(dto.getSex())){
            user.setSex(dto.getSex());
        }
        if (StringUtils.isEmpty(dto.getHeadImg())
                && StringUtils.isEmpty(dto.getName())
                && StringUtils.isEmpty(dto.getNickname())
                && StringUtils.isEmpty(dto.getSex())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(user);
        return ResponseResult.okResult(user);
    }

    @Override
    public ResponseResult loadAllInfo() {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer id = user.getId();
        User userInfo = getById(id);
        if (userInfo == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setHeadImg(userInfo.getHeadPic());
        userInfoVo.setUsername(userInfo.getUsername());
        userInfoVo.setNickname(userInfo.getNickName());
        userInfoVo.setSex(userInfo.getSex());
        userInfoVo.setName(userInfo.getName());
        ResponseResult responseResult = ResponseResult.okResult(userInfoVo);
        responseResult.setHost(fdfsUrl);
        return responseResult;
    }

}
